package api

import (
	"certificate/service"
	"certificate/vo"
	"github.com/gin-gonic/gin"
)

// DouYinReady
// @Tags "抖音"
// @Param code query string true "code"
// @Success 200 {object} vo.DouYinCertificatePrepare
// @Router /douyin/ready [get]
// @Summary "准备验券"
// @Description "抖音验券准备"
// 100011 入参校验失败
// 100012 创建抖音对象失败
// 100013 准备验券失败
func DouYinReady(c *gin.Context) {

	certificatePrepare := vo.DouYinCertificatePrepare{}

	err := c.ShouldBindJSON(&certificatePrepare)

	if err != nil {
		ResponseError(c, err.Error(), 100011)
		return
	}

	douyin, err := service.NewDouyin()
	if err != nil {
		ResponseError(c, err.Error(), 100012)
		return
	}
	prepare, err := douyin.Prepare(certificatePrepare)
	if err != nil {
		ResponseError(c, err.Error(), 100013)
		return
	}
	ResponseSuccess(c, prepare)

}

// DouYinVerify
// @Tags "抖音"
// @Param data body vo.DouYinCertificateVerify true "验券数据"
// @Success 200 {object} vo.DouYinCertificateVerify
// @Router /douyin/verify [post]
// @Summary "验券"
// @Description "抖音验券"
// 100021 入参校验失败
// 100022 创建抖音对象失败
// 100023 验券失败
func DouYinVerify(c *gin.Context) {

	certificateVerify := vo.DouYinCertificateVerify{}
	err := c.ShouldBindJSON(&certificateVerify)

	if err != nil {
		ResponseError(c, err.Error(), 100021)
		return
	}

	douyin, err := service.NewDouyin()
	if err != nil {
		ResponseError(c, err.Error(), 100022)
		return
	}
	verify, err := douyin.Verify(certificateVerify)
	if err != nil {
		ResponseError(c, err.Error(), 100023)
		return
	}
	ResponseSuccess(c, verify)
}

// DouYinCancel
// @Tags "抖音"
// @Param data body vo.DouYinCertificateCancel true "取消验券数据"
// @Success 200 {object} vo.DouYinCertificateCancel
// @Router /douyin/cancel [post]
// @Summary "取消验券"
// @Description "抖音取消验券"
// 100031 入参校验失败
// 100032 创建抖音对象失败
// 100033 取消验券失败
func DouYinCancel(c *gin.Context) {
	certificateCancel := vo.DouYinCertificateCancel{}
	err := c.ShouldBindJSON(&certificateCancel)

	if err != nil {
		ResponseError(c, err.Error(), 100031)
		return
	}

	douyin, err := service.NewDouyin()
	if err != nil {
		ResponseError(c, err.Error(), 100032)
		return
	}
	cancel, err := douyin.Cancel(certificateCancel)
	if err != nil {
		ResponseError(c, err.Error(), 100033)
		return
	}
	ResponseSuccess(c, cancel)
}

func DouYinGet(c *gin.Context) {
	certificateGet := vo.DouYinCertificateGet{}
	err := c.ShouldBindJSON(&certificateGet)
	if err != nil {
		ResponseError(c, err.Error(), 100041)
		return
	}

	douyin, err := service.NewDouyin()
	if err != nil {
		ResponseError(c, err.Error(), 100042)
		return
	}
	get, err := douyin.Get(certificateGet)
	if err != nil {
		ResponseError(c, err.Error(), 100043)
		return
	}
	ResponseSuccess(c, get)
}
